﻿module JsExecutionTest

open NUnit.Framework
open JsAst
open JsEngine
open JsTypes
open ScriptingExposedAttribute
open HostObjectReflection
open JsEval

let x = ref ""

type DummyHostObject() =
    [<ScriptingExposed>]
    member public this.alert(str:string) =        
        do x := str


[<TestFixture>]
type testJsEngine() =

    [<Test>]
    member self.testExecJs() =
        do x := ""
        let defaultEnv = createTopFrame [("window", Object(HostObjectWrapper(DummyHostObject())))] Undefined
        let ast = [ExpressionStatement(CallExpression(PropertyExpression(Name("window"), "alert"), [StringLiteral("Hello, world!")]))]
        let result = evalStmts ast defaultEnv
        Assert.AreEqual("Hello, world!", !x)

    [<Test>]
    member self.testExecJsFunction() =
        do x := ""
        let defaultEnv = createTopFrame [("window", Object(HostObjectWrapper(DummyHostObject())))] Undefined
        let ast = [ExpressionStatement(CallExpression(
                                FunctionExpression([],
                                    [ExpressionStatement(
                                        CallExpression(
                                            PropertyExpression(Name("window"), "alert"), 
                                            [StringLiteral("Hello, darkness!")]))]), []))]
        let result = evalStmts ast defaultEnv
        Assert.AreEqual("Hello, darkness!", !x)

    [<Test>]
    member self.testExecJsEngine() =
        do x := ""
        let engine = JsEngine([("window", Object(HostObjectWrapper(DummyHostObject())))])
        let ast = [ExpressionStatement(CallExpression(PropertyExpression(Name("window"), "alert"), [StringLiteral("Hello, world!")]))]
        let result = engine.evalAst ast
        Assert.AreEqual("Hello, world!", !x)